home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / fclose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  789 b   |  38 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <memory.h>
  7.  
  8. int fclose(fp)
  9. register FILE *fp;
  10. {
  11.     register unsigned int f;
  12.     register int error = 0;
  13.     
  14.     if(fp == NULL)
  15.     return(EOF);        /* NULL file pointer file */
  16.     f = fp->_flag;
  17.     if((f & (_IORW | _IOREAD | _IOWRT)) == 0)
  18.     return(EOF);        /* file not open! */
  19.     if(f & (_IOWRT | _IORW))        /* only bother flushing for write */
  20.     error = fflush(fp);
  21.     if(!(f & _IOMYBUF)) /* throw away non-standard buffer */
  22.     {
  23.     fp->_base = NULL;
  24.     fp->_ptr = NULL;
  25.     fp->_bsiz = 0;
  26.     }
  27.     else
  28.     {
  29.     free(fp->_base);
  30.     fp->_base = NULL;
  31.     fp->_ptr = NULL;
  32.     fp->_bsiz = 0;
  33.     }
  34.     fp->_flag = 0;            /* clear status */
  35.     error |= close(fp->_file);
  36.     return(error ? EOF : 0);
  37. }
  38.